import unittest
import time
from ChessBoard import *

class TestEnd2End(unittest.TestCase):
    
    def setUp(self):
         """ setUp reads and loads moves from data files """
         self.f = open('moves.txt', 'r')
         self.f2 = open('moves2.txt', 'r')
         self.chessboard = ChessBoard()
         self.chessboard.resetBoard()                                                     
         self.moves = []                                                                  
         self.moves2 = []
         print "Loading moves..."                                                    
         for line in self.f:
            self.moves.extend((line.rstrip()).split(" "))                                
         for line in self.f2:
            self.moves2.extend((line.rstrip()).split(" "))                                
         print "Moves loaded"  
         self.results=[]

    def testInitialization(self):
        """ initialization test """
        self.assertTrue(self.chessboard)
        self.assertTrue(self.moves)
        self.assertTrue(self.moves2)
        self.assertIsInstance(self.moves,list)
        self.assertIsInstance(self.moves2,list)

    def testGameplay1(self):
        """ Attempts to simulate the game loaded from files. When move fails - prints out a reason """
        games = [self.moves, self.moves2]
        for game in games:
            i = 0
            for move in game:
                print "Move number: ", i
                i += 1
                self.chessboard.printBoard()
                result = self.chessboard.addTextMove(move)
                if result == False:
                    print "Move failed. Reasons explained:"
                    print "1=INAVLID_MOVE,2=INVALID_COLOR,3=INVALID_FROM_LOCATION,4=INVALID_TO_LOCATION,5=MUST_SET_PROMOTION,5=GAME_IS_OVER"
                    print "Reason code: %s " % self.chessboard.getReason()
                self.results.append(result)
                print result
                time.sleep(0.1)
            print "results from game: %s" % game
            print self.results

    def tearDown(self):
        """ Cleanup """
        self.chessboard = None
        self.moves = None
        self.moves2 = None
        self.f.close()
        self.f2.close()

if __name__ == "__main__":
    unittest.main()

